package com.pfzy.lib.sdk.permission;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

/**
 * 权限设置页面
 */
final class PermissionSettingPage {
    private static final String MARK = Build.MANUFACTURER.toLowerCase();

    PermissionSettingPage() { }

    public static void start(Context context, boolean newTask, boolean useSystemDefault) {
        Intent intent = null;
        if (useSystemDefault) {
            intent = google(context);
        } else {
            if (MARK.contains("huawei")) {
                intent = huawei(context);
            } else if (MARK.contains("xiaomi")) {
                intent = xiaomi(context);
            } else if (MARK.contains("oppo")) {
                intent = oppo(context);
            } else if (MARK.contains("vivo")) {
                intent = vivo(context);
            } else if (MARK.contains("meizu")) {
                intent = meizu(context);
            }
            if (intent == null || !hasIntent(context, intent)) {
                intent = google(context);
            }
        }
        if (newTask) {
            intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
        }
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            Intent intent2 = google(context);
            context.startActivity(intent2);
        }
    }

    private static Intent google(Context context) {
        Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        return intent;
    }

    private static Intent huawei(Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity"));
        if (!hasIntent(context, intent)) {
            intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity"));
            if (!hasIntent(context, intent)) {
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity"));
            }
        }
        return intent;
    }

    private static Intent xiaomi(Context context) {
        Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
        intent.putExtra("extra_pkgname", context.getPackageName());
        if (!hasIntent(context, intent)) {
            intent.setPackage("com.miui.securitycenter");
            if (!hasIntent(context, intent)) {
                intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                if (!hasIntent(context, intent)) {
                    intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
                }
            }
        }
        return intent;
    }

    private static Intent oppo(Context context) {
        Intent intent = new Intent();
        intent.putExtra("packageName", context.getPackageName());
        intent.setClassName("com.color.safecenter", "com.color.safecenter.permission.floatwindow.FloatWindowListActivity");
        if (!hasIntent(context, intent)) {
            intent.setClassName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity");
            if (!hasIntent(context, intent)) {
                intent.setClassName("com.oppo.safe", "com.oppo.safe.permission.PermissionAppListActivity");
            }
        }
        return intent;
    }

    private static Intent vivo(Context context) {
        Intent intent = new Intent();
        intent.setClassName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.FloatWindowManager");
        intent.putExtra("packagename", context.getPackageName());
        if (!hasIntent(context, intent)) {
            intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.safeguard.SoftPermissionDetailActivity"));
        }
        return intent;
    }

    private static Intent meizu(Context context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.putExtra("packageName", context.getPackageName());
        intent.setComponent(new ComponentName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"));
        return intent;
    }

    private static boolean hasIntent(Context context, Intent intent) {
        return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    }
}
